﻿using System;
using System.Net;

namespace Heimdallr.Net
{
    /// <summary>
    /// Provides a set of extension methods for <see cref="IPAddress"/>.
    /// </summary>
    public static class IPAddressExtensions
    {
        /// <summary>
        /// Converts an IPv4 address to long integer.
        /// </summary>
        /// <param name="ip">An IP address.</param>
        /// <returns>A long integer represents current IP address.</returns>
        /// <exception cref="ArgumentNullException">Parameter <paramref name="ip"/> is null.</exception>
        public static long ToInt64(this IPAddress ip)
        {
            if (ip == null)
                throw new ArgumentNullException("ip");

            byte[] bytes = ip.GetAddressBytes();

            return Convert.ToInt64((Convert.ToUInt32(bytes[3]) << 24) | (Convert.ToUInt32(bytes[2]) << 16) | (Convert.ToUInt32(bytes[1]) << 8) | Convert.ToUInt32(bytes[0]));
        }
    }
}